package com.guldan.jaina.common.filter;

import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.rpc.*;
import com.guldan.jaina.dto.users.UsersDTO;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Silas.
 * Date: 2016/12/10
 * Time: 15:25
 */
public class DubboProviderFilter implements Filter {

    public Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {


        Long cost = System.currentTimeMillis();


        //logger.error("DubboProviderFilter-" + JSONObject.fromObject(RpcContext.getContext().getAttachments()).toString());

        //初始化获取client用户信息
        UsersDTO usersDTO = DubboContextRecorder.initUserDTOAttFrom();


        Result result = invoker.invoke(invocation);

        String i = "if." + invocation.getInvoker().getInterface().getName() + "." + invocation.getMethodName() + "-" + NetUtils.getLocalHost();
        cost = System.currentTimeMillis() - cost;
        logger.info(String.format("if:%s cost:%s", i, cost));
        /*if (usersDTO != null)
            logger.debug("[InterfaceName]: " + invocation.getInvoker().getInterface().getName() + "." + invocation.getMethodName() + " [cost = " + cost + "] [DubboProviderFilter] invoke [UsersDTO] " + JSONObject.fromObject(usersDTO).toString());
        else
            logger.debug("[InterfaceName]: " + invocation.getInvoker().getInterface().getName() + "." + invocation.getMethodName() + " [cost = " + cost + "] [DubboProviderFilter] invoke [UsersDTO] is null");
*/

        return result;
    }
}
